Java中的死锁指的就是一种多于两个线程永远阻塞的特殊状况。Java中的死锁状态至少需要多于两个线程以及资源的时候才会产生。这里,我写了一个产生死锁的程序,并且讲下如何分析死锁。
首先来看一下产生死锁的程序:
package com.sapphire.threads;
public class ThreadDeadlock {
public static void main(String[] args) throws InterruptedException {
Object obj1 = new Object();
Object obj2 = new Object();
Object obj3 = new Object();
Thread t1 = new Thread(new SyncThread(obj1, obj2), "t1");
Thread t2 = new Thread(new SyncThread(obj2, obj3), "t2");
Thread t3 = new Thread(new SyncThread(obj3, obj1), "t3");
t1.start();
Thread.sleep(5000);
t2.start();
Thread.sleep(5000);
t3.start();
}
}
class SyncThread implements Runnable{
private Object obj1;
private Object obj2;
public SyncThread(Object o1, Object o2){
this.obj1=o1;
this.obj2=o2;
}
@Override
public void run() {
String name = Thread.currentThread().getName();
System.out.println(name + " acquiring lock on "+obj1);
synchronized (obj1) {
System.out.println(name + " acquired lock on "+obj1);
work();
System.out.println(name + " acquiring lock on "+obj2);
synchronized (obj2) {
System.out.println(name + " acquired lock on "+obj2);
work();
}
System.out.println(name + " released lock on "+obj2);
}
System.out.println(name + " released lock on "+obj1);
System.out.println(name + " finished execution.");
}
private void work() {
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
在上面的程序中,我们看到SyncThread
是通过实现了Runnable
接口来实现的多线程的,它内部包含两个Object
对象,通过synchronized
代码块 来获取对象锁。
在主方法中,我定义了3个线程,分别是t1
,t2
和t3
,运行的过程中,会先请求第一个对象的锁,获取之后,再请求第二个对象的锁。所以当一个线程尝试获取第二个对象的锁,而第二个对象的锁被其他线程占有的时候,第一个线程就会进入wait
状态,而第二个线程所需要的资源也在由第三个线程所锁定,所以三个线程构成的循环构成了死锁。
如果我执行了上面的程序,会有如下输出,但是程序不会结束,因为线程死锁而导致的线程无法结束。
t1 acquiring lock on java.lang.Object@fdfdda6
t1 acquired lock on java.lang.Object@fdfdda6
t2 acquiring lock on java.lang.Object@51dca821
t2 acquired lock on java.lang.Object@51dca821
t3 acquiring lock on java.lang.Object@25c